package com.hndzyu.yd.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;


import com.hndzyu.yd.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/11/6.
 */
public class CheckPopupWindow extends PopupWindow {
    @BindView(R.id.reason)
    EditText reason;
    @BindView(R.id.tv_true)
    TextView tvTrue;
    @BindView(R.id.tv_false)
    TextView tvFalse;
    private View contextView;
    private Activity mContext;
    private CheckListener mListener;

    public void setListener(CheckListener listener) {
        mListener = listener;
    }

    public CheckPopupWindow(Activity context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contextView = inflater.inflate(R.layout.popup_check, null);
        mContext = context;
        this.setContentView(contextView);
        ButterKnife.bind(this, contextView);
        tvTrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("".equals(reason.getText().toString())){
                    Toast.makeText(mContext,"请填写未通过原因",Toast.LENGTH_SHORT).show();
                } else {
                    mListener.checkTrue(reason.getText().toString());
                }
            }
        });
        tvFalse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(30000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.my_dialog);
    }

    public void showDown(View view) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.7f;
        mContext.getWindow().setAttributes(lp);
        this.showAtLocation(view, Gravity.CENTER_HORIZONTAL, 0, 0);
    }
    public interface CheckListener{
        void checkTrue(String reason);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 1f;
        mContext.getWindow().setAttributes(lp);
    }
}
